#!/bin/sh

config_file_path()
{
    local FileName=$1
    local ParameterName=$2
    local ConfigFileName=$3
    local DirName

    echo -n " --- Configure path to $FileName: "
    for DirName in /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin
    do
        if test -x "$DirName/$FileName"
        then
            if test "$FileName" = "logger"
            then
                echo $ParameterName=\"$DirName/$FileName -t flex-fw -s\" >$ConfigFileName
            else
                echo $ParameterName=$DirName/$FileName >$ConfigFileName
            fi
            echo found $DirName/$FileName.
            return 0
        fi
    done
    echo fail!
    return 1
}

configure()
{
    echo "Tuning flex-fw:"
    config_file_path iptables flexfw_IPTABLES /etc/flex-fw/vars/base/iptables
    config_file_path mktemp flexfw_MKTEMP /etc/flex-fw/vars/base/mktemp
    config_file_path logger flexfw_LOGGER /etc/flex-fw/vars/base/logger
    config_file_path flock flexfw_FLOCK /etc/flex-fw/vars/base/flock
    config_file_path conntrack flexfw_CONNTRACK /etc/flex-fw/vars/base/conntrack
    echo "Tuning is done."

    echo "Check symbolic links:"
    echo -n " --- for /etc/flex-fw/services/local.lo: "
    if test -L /etc/flex-fw/rc.d/S000.local.lo
    then
        echo "Ok."
    else
        ln -s /etc/flex-fw/services/local.lo /etc/flex-fw/rc.d/S000.local.lo
        echo "Ok."
    fi

    echo -n " --- for /etc/flex-fw/services/local.icmp: "
    if test -L /etc/flex-fw/rc.d/S001.local.icmp
    then
        echo "Ok."
    else
        ln -s /etc/flex-fw/services/local.icmp /etc/flex-fw/rc.d/S001.local.icmp
        echo "Ok."
    fi

    echo -n " --- for /etc/flex-fw/services/local.ssh: "
    if test -L /etc/flex-fw/rc.d/S002.local.ssh
    then
        echo "Ok."
    else
        ln -s /etc/flex-fw/services/local.ssh /etc/flex-fw/rc.d/S002.local.ssh
        echo "Ok."
    fi

    echo -n " --- for /etc/flex-fw/services/syslog.filter: "
    if test -L /etc/flex-fw/rc.d/S998.syslog.filter
    then
        echo "Ok."
    else
        ln -s /etc/flex-fw/services/syslog.filter /etc/flex-fw/rc.d/S998.syslog.filter
        echo "Ok."
    fi

    echo -n " --- for /etc/flex-fw/services/syslog: "
    if test -L /etc/flex-fw/rc.d/S999.syslog
    then
        echo "Ok."
    else
        ln -s /etc/flex-fw/services/syslog /etc/flex-fw/rc.d/S999.syslog
        echo "Ok."
    fi

    cat <<END_OF_MESSAGE

Installation is complete!

WARNING! Default firewall policy is 'ACCEPT'.
To change this behaviour you should modify files in the /etc/flex-fw/policy directory.

END_OF_MESSAGE
}

if test "$1" = "configure"
then
    configure
fi

# end of file